package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model) {
        List<Region> hotRegions = regionService.queryHotRegions();
        model.addAttribute("hotRegions", hotRegions);
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Long regionId, Model model) {
        //所有的目的地
        List<Destination> destinations = destinationService.getDestByRegionId(regionId);
        List<Destination> leftDests = destinations.subList(0, destinations.size() / 2 + 1);
        model.addAttribute("leftDests", leftDests);
        List<Destination> rightDests = destinations.subList(destinations.size() / 2 + 1, destinations.size());
        model.addAttribute("rightDests", rightDests);
        model.addAttribute("regionId", regionId);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Long id, Model model) {
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        //catalogs  details
        //额外SQL  查询所有的details
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(id);
        model.addAttribute("catalogs", catalogs);
        //strategyDetails  广州攻略点击量前三的攻略集合
        model.addAttribute("strategyDetails", strategyDetailService.getViewNumTop3(id));
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        //被选中的catalog  和detail
        //qo.getCatalogId();  在catalogs中

        //为了严谨,所以需要进行判断,防止没有数据
        if (catalogs.size() > 0) {
            for (StrategyCatalog catalog : catalogs) {
                if (catalog.getId() == qo.getCatalogId()) {
                    model.addAttribute("catalog", catalog);
                    //第一个分类明细的  detail
                    List<StrategyDetail> details = catalog.getDetails();
                    if (details.size() > 0) {
                        StrategyDetail detail = details.get(0);
                        //还需要设置内容
                        detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                        model.addAttribute("detail", detail);
                    }
                }
            }
        }


        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travel(Model model, @ModelAttribute("qo") TravelQuery qo) {
        //pageInfo
        model.addAttribute("pageInfo", travelService.query(qo));

        return "destination/travelTpl";
    }
}
